package com.lh.ssm.web;

import com.google.gson.Gson;
import com.lh.ssm.domain.Address;
import com.lh.ssm.domain.Cart;
import com.lh.ssm.domain.Order;
import com.lh.ssm.domain.User;
import com.lh.ssm.service.AddressService;
import com.lh.ssm.service.CartService;
import com.lh.ssm.service.OrderService;
import com.lh.ssm.vo.WeiXin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private CartService cartService;

    //展示某个用户的订单
    @RequestMapping("/orderShow")
    public String show(HttpSession session, HttpServletRequest req) {
        //判断用户是否登录
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            session.setAttribute("msg", "请先登录");
            return "login";
        }
        int uid = user.getId();
        //根据用户id查询订单
        List<Order> orders = orderService.findOrdersByUid(uid);

        //跳转到订单列表页面
        req.setAttribute("ordersList", orders);
        return "orderList";
    }

    //预处理订单：也就是在创建订单之前,检查某个客户的购物车信息,以及地址信息
    @GetMapping("/ordePreView")
    public String preView(int uid, HttpServletRequest req) {
        //根据用户id,查找对应的购物车信息以及地址信息
        List<Address> addressList = addressService.findAddressByUid(uid);
        List<Cart> cartList = cartService.showCartByUid(uid);

        //把查询到的购物车和地址信息,传递order页面
        req.setAttribute("cartList", cartList);
        req.setAttribute("addressList", addressList);
        return "order";

    }

    //创建订单:1.要插入到shop_order表;2.插入shop_item表;3.清空购物车
    @GetMapping("/orderCreate")
    public String create(String uid, String sum, String aid) {
        //根据获得的请求参数，创建订单
        orderService.createOrder(uid, sum, aid);
        return "forward:/orderShow";
    }

    //根据订单号,查询订单详细信息
    @GetMapping("/orderDetail")
    public String detail(String oid, HttpServletRequest req) {
        //根据订单号,查询对应的订单信息
        Order order = orderService.findOrderById(oid);
        //将total金额转为以分为单位的金额!为了微信支付使用,微信支付的金额不能带小数点,必须是一个整数值!
        BigDecimal baseMoney = new BigDecimal(100);
        BigDecimal totalDecimal = order.getTotal().multiply(baseMoney);
        order.setTotalFee(totalDecimal.longValue());

        //跳转到订单详情页
        req.setAttribute("order", order);
        return "orderDetail";
    }


    //处理支付成功后的订单状态:把未支付状态改为已支付
    @GetMapping("/orderSuccess")
    public String success(String oid, HttpServletRequest req) {
        //1.获取订单id

        //result来自于http://ceshi.qfjava.cn/payment/weixinpay测试服务器!
        //这是一个json字符串
        String result = req.getParameter("result");

        //2.判断支付成功与否的结果
        Gson gson = new Gson();
        WeiXin weiXin = gson.fromJson(result, WeiXin.class);
        String resultCode = weiXin.getResult().getResult_code();

        if (resultCode != null && "SUCCESS".equals(resultCode)) {
            //更改订单的支付状态

            orderService.updatePayStateById(oid);

            return "forward:/orderShow";
        } else {
            HttpSession session = req.getSession();
            session.setAttribute("msg", "订单:" + oid + "支付失败!");
            return "message";
        }

    }


}